//
//  TableViewCell.m
//  Product
//
//  Created by 吴志勇 on 2020/4/2.
//  Copyright © 2020 wzy. All rights reserved.
//

#import "TableViewCell.h"
#import <UIImageView+WebCache.h>
#import "Masonry.h"


@interface TableViewCell()

/**  */
@property (nonatomic, strong) UIView *headerView;

/**  */
@property (nonatomic, strong) UIImageView *centerView;

/**  */
@property (nonatomic, strong) UIView *btnView;

/**  */
@property (nonatomic, assign) CGFloat imgViewW;

@end

@implementation TableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    self.imgViewW = self.contentView.bounds.size.width - 20;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:@"yjCellId"]){
        [self setUI];
    }
    return self;
}

//
- (void)setUI{
//    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.right.mas_equalTo(self.contentView);
//        make.top.mas_equalTo(10);
//        make.height.mas_equalTo(30);
//    }];
    
    [self.centerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.left.mas_equalTo(10);
    }];
    
    [self.btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.centerView.mas_bottom).mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.height.mas_equalTo(10);
        make.right.mas_equalTo(-10);
        make.bottom.mas_equalTo(self.contentView.mas_bottom).mas_offset(-10);
    }];
    
}

- (UIView *)headerView{
    if (_headerView == nil) {
        _headerView = [[UIView alloc] init];
        _headerView.backgroundColor = [UIColor redColor];
        [self.contentView addSubview:_headerView];
//        _headerView = headerView;
    }
    return _headerView;
}

- (UIImageView *)centerView{
    if (_centerView == nil) {
        _centerView = [[UIImageView alloc] init];
        _centerView.backgroundColor = [UIColor blueColor];
        [self.contentView addSubview:_centerView];
    }
    return _centerView;
}

- (UIView *)btnView{
    if (_btnView == nil) {
        _btnView = [[UIView alloc] init];
        _btnView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_btnView];
    }
    return _btnView;
}

- (void)setModel:(YJModel *)model{
    _model = model;
//    self.userName.text = model.nickName;
//    self.sendTime.text = [self compareCurrentTime:model.createTime];
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://yuejifile.oss-cn-beijing.aliyuncs.com/yuejifile/%@",model.headPortrait]];
//    [self.headerImage sd_setImageWithURL:url placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];
//    self.headerImage.layer.cornerRadius = 15;
//    self.headerImage.layer.masksToBounds = YES;
//
//    if (model.isLikeStatus) {
//        self.plBtn.userInteractionEnabled = YES;
//    }else{
//        self.plBtn.userInteractionEnabled = NO;
//    }
//
//
//    if (model.time) {
//        self.timeCity.text = [NSString stringWithFormat:@"%@,%@",model.time,model.city];
//    }else{
//        self.timeCity.text = [NSString stringWithFormat:@"不限时间,%@",model.city];
//    }
//    self.exceptLabel.text = [NSString stringWithFormat:@"期望对象:%@",model.expected];
//
//    for (UIImageView *subImageView in self.LikeImageView.subviews) {
//        [subImageView removeFromSuperview];
//    }
//
//
//    NSLog(@"%lf",self.imgViewW);
//
    if (model.imagesList.count == 0) {
        self.centerView.hidden = YES;
        self.centerView.autoresizingMask = 
        [self.centerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(0).priorityHigh();
        }];
    }else if (model.likeHeadPortraitList.count <= 4){
        self.centerView.hidden = NO;
        [self.centerView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(600).priorityHigh();
        }];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.backgroundColor = [UIColor blackColor];
        [self.centerView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(10);
            make.size.mas_equalTo(CGSizeMake(300, 300));
            make.left.mas_equalTo(40);
        }];
    }else{
    }

    [super updateConstraints];
//
//        self.LikeImageView.hidden = NO;
//        self.likeImageH.constant = 300;
//        NSLog(@"%@",model.imagesList);
//        for (int i = 0; i < model.imagesList.count; i++) {
//            UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(distance+distance*i, distance, imageViewW, imageViewW)];
//            [self.LikeImageView addSubview:imageView];
//            NSURL *likeurl = [NSURL URLWithString:[NSString stringWithFormat:@"https://yuejifile.oss-cn-beijing.aliyuncs.com/yuejifile/%@",model.imagesList[i]]];
//            NSLog(@"%@",likeurl);
//            [self.headerImage sd_setImageWithURL:likeurl placeholderImage:nil options:SDWebImageAllowInvalidSSLCertificates];
//        }
//
//    }
//
}


-(NSString *)compareCurrentTime:(NSString *)str
{
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    //八小时时区
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate: timeDate];
    NSDate *mydate = [timeDate dateByAddingTimeInterval: interval];
    NSDate *nowDate =[[NSDate date] dateByAddingTimeInterval: interval];
    // 两个时间间隔
    NSTimeInterval timeInterval= [mydate timeIntervalSinceDate:nowDate];
    timeInterval = -timeInterval;
    
//    NSLog(@"时间是%f",timeInterval);
    
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = timeInterval/(60*60)) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = timeInterval/(246060)) < 30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = timeInterval/(24606030)) < 12){
        if (temp == 0 ) {
            temp += 1;
        }
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = timeInterval/(24606030*12);
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return result;
}

- (IBAction)dianzanClicl:(id)sender {
    if (self.dzClick) {
        self.dzClick();
    }
    
}
- (IBAction)pinlunClik:(id)sender {
    if (self.plClick) {
        self.plClick();
    }
}
- (IBAction)baomingClick:(id)sender {
    if (self.joinClick) {
        self.joinClick();
    }
}


- (void)setFrame:(CGRect)frame
{
    frame.origin.x += 10;//往左偏移10
    frame.size.width -= 20;//往左偏移10,会有看不见的，宽度剪20 悬浮在中间
    frame.size.height -=10;//让cell下面有个10的间距
    [super setFrame:frame];
}

@end
